package com.linchong.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.linchong.entity.Client;
import com.linchong.entity.User;

public class Demo {
	
	@Test
	public void fun() {
		Configuration conf = new Configuration().configure();

		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();

		Transaction tx = session.beginTransaction();
		// ----------------------------------
		// 面向对象操作数据库：
		User c = new User();
		c.setUsername("网易云音乐");

		session.save(c);
		// ----------------------------------
		tx.commit();
		session.close();
		sessionFactory.close();
	}
	
	@Test
	public void fun1() {
		Configuration conf = new Configuration().configure();
        conf.addResource("com/linchong/entity/Client.hbm.xml");
		
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();

		Transaction tx = session.beginTransaction();
		// ----------------------------------
		// 面向对象操作数据库：
		Client c = new Client();
		c.setName("酷我音乐");
        c.setAddr("北京");
        c.setAge(10);
		session.save(c);
		// ----------------------------------
		tx.commit();
		session.close();
		sessionFactory.close();
	}
	
	

}
